package ManHinhChinh;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.TableColumn;

import ManHinhChinh.KiemTra_Search;
import ManHinhChinh.TaoKetNoi;
public class TimKiemKhachHang {

	protected Shell shlTmKimThng;
	private int i=1;
	public String tam = null;
	private Table table;
	
	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shlTmKimThng.open();
		shlTmKimThng.layout();
		while (!shlTmKimThng.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 * @wbp.parser.entryPoint
	 */
	protected void createContents() {
		shlTmKimThng = new Shell();
		shlTmKimThng.setSize(707, 398);
		shlTmKimThng.setText("T\u00ECm Ki\u1EBFm Th\u00F4ng Tin Kh\u00E1ch H\u00E0ng");
		
		final Label lblTimKiem = new Label(shlTmKimThng, SWT.NONE);
		lblTimKiem.setText("Nh\u1EADp M\u00E3 Kh\u00E1ch H\u00E0ng");
		lblTimKiem.setFont(SWTResourceManager.getFont("Times New Roman", 13, SWT.NORMAL));
		lblTimKiem.setBounds(46, 54, 153, 23);
		
		final Text txtTimKiem;
		txtTimKiem = new Text(shlTmKimThng, SWT.BORDER | SWT.SEARCH | SWT.MULTI);
		txtTimKiem.setToolTipText("Nhập Thông tin cần tìm kiếm");
		txtTimKiem.setBounds(225, 54, 216, 21);
		
		Button RBtnTenKhachHang = new Button(shlTmKimThng, SWT.RADIO);
		RBtnTenKhachHang.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				i=2;
				lblTimKiem.setText("Nhập Tên Khách Hàng");
				lblTimKiem.setFont(SWTResourceManager.getFont("Times New Roman", 13, SWT.NORMAL));
			}
		});
		RBtnTenKhachHang.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.NORMAL));
		RBtnTenKhachHang.setBounds(336, 10, 122, 27);
		RBtnTenKhachHang.setText("T\u00EAn Kh\u00E1ch H\u00E0ng");
		
		Button RBtnMaKhachHang = new Button(shlTmKimThng, SWT.RADIO);
		RBtnMaKhachHang.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				i=1;
				lblTimKiem.setText("Nhập Mã Khách Hàng");
				lblTimKiem.setFont(SWTResourceManager.getFont("Times New Roman", 13, SWT.NORMAL));	
			}
		});
		RBtnMaKhachHang.setSelection(true);
		RBtnMaKhachHang.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.NORMAL));
		RBtnMaKhachHang.setBounds(179, 10, 122, 27);
		RBtnMaKhachHang.setText("M\u00E3 Kh\u00E1ch H\u00E0ng");
		
		table = new Table(shlTmKimThng, SWT.BORDER | SWT.FULL_SELECTION| SWT.VIRTUAL);
		table.setBackground(SWTResourceManager.getColor(SWT.COLOR_GRAY));
		table.setBounds(10, 164, 671, 83);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		table.setItemCount(1);
		
		Label lblNewLabel = new Label(shlTmKimThng, SWT.NONE);
		lblNewLabel.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.NORMAL));
		lblNewLabel.setBounds(37, 14, 117, 23);
		lblNewLabel.setText("T\u00ECm Ki\u1EBFm Theo : ");
		
		Button btnTimKiem = new Button(shlTmKimThng, SWT.NONE);
		btnTimKiem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				 String _txtTimKiem = txtTimKiem.getText().trim();
				
				// xet tem khung text co Rong hay khong
				if(txtTimKiem.getText().equals(""))
				{
					MessageBox Msb = new MessageBox(shlTmKimThng,SWT.ICON_ERROR|SWT.OK );
					Msb.setText("Error");
					Msb.setMessage("Bạn chưa nhập dữ liệu cần tìm.... ");
					Msb.open();
					return;
				
				}
				KiemTra_Search kt= new KiemTra_Search();
				kt.TestSearch(_txtTimKiem,"[QuanLyMuaBanHangHoa].[dbo].[KHACH_HANG]",1);
				tam = kt._search;
				if(i==1)
				{
					
							table.setItemCount(0);
							TaoKetNoi s = new TaoKetNoi();
							 Connection con = s.TaoLienKet();

								try {
									Statement stm = con.createStatement();
									ResultSet rs = stm.executeQuery("SELECT * FROM [QuanLyMuaBanHangHoa].[dbo].[KHACH_HANG] WHERE MAKHACHHANG='"+tam+"'");
									while (rs.next())
									{
										TableItem item= new TableItem(table,SWT.NONE);
										item.setText(new String[] {rs.getString(1), 
												rs.getString(2), 
												rs.getString(3), 
												rs.getString(4), 
												rs.getString(5),
										});	
										con.close();
									}						
								} catch (SQLException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}	
						}
				
				
			}
		});
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		btnTimKiem.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.NORMAL));
		btnTimKiem.setImage(SWTResourceManager.getImage("D:\\Java_WorkSpace\\_QuanLyBanHang\\image\\PrintPreview.ico"));
		btnTimKiem.setBounds(164, 98, 111, 40);
		btnTimKiem.setText("T\u00ECm Ki\u1EBFm");
		
		
		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.CENTER);
		tblclmnNewColumn.setWidth(101);
		tblclmnNewColumn.setText("Mã Khách Hàng");
		
		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.CENTER);
		tblclmnNewColumn_1.setWidth(139);
		tblclmnNewColumn_1.setText("Họ Tên");
		
		TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.CENTER);
		tblclmnNewColumn_2.setWidth(181);
		tblclmnNewColumn_2.setText("Địa Chỉ");
		
		TableColumn tblclmnNewColumn_3 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_3.setWidth(100);
		tblclmnNewColumn_3.setText("Số Điện Thoại");
		
		TableColumn tblclmnNewColumn_4 = new TableColumn(table, SWT.CENTER);
		tblclmnNewColumn_4.setWidth(143);
		tblclmnNewColumn_4.setText("Email");
		
		
		Button BtnTimKiemSuaTT = new Button(shlTmKimThng, SWT.NONE);
		BtnTimKiemSuaTT.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.NORMAL));
		BtnTimKiemSuaTT.setImage(SWTResourceManager.getImage("D:\\Java_WorkSpace\\_QuanLyBanHang\\image\\undo.ico"));
		BtnTimKiemSuaTT.setBounds(146, 296, 129, 40);
		BtnTimKiemSuaTT.setText("Sửa Thông Tin");
		
		Button btnTimKiemXoaTT = new Button(shlTmKimThng, SWT.NONE);
		btnTimKiemXoaTT.setImage(SWTResourceManager.getImage("D:\\Java_WorkSpace\\_QuanLyBanHang\\image\\Xóa.png"));
		btnTimKiemXoaTT.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.NORMAL));
		btnTimKiemXoaTT.setText("Xóa Thông Tin");
		btnTimKiemXoaTT.setBounds(376, 296, 122, 40);
		
		

	}
}
